package com.alibaba.dingtalk.openapi.helper;

import com.alibaba.dingtalk.openapi.exception.OApiException;
import com.alibaba.dingtalk.openapi.exception.OApiResultException;
import com.alibaba.dingtalk.openapi.utils.HttpHelper;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.pro.constant.I18nStringConstant;

/**
 * @author kaka
 */
public class MicroAppHelper {
	
	/**
	 * 获取企业设置的微应用可见范围
	 * 以下几种情况都是全公司:
	 *	{"deptVisibleScopes":[],"isHidden":false,"userVisibleScopes":[]}  没有选任何部门和人
	 *	{"deptVisibleScopes":"1","isHidden":false}						      微应用添加时选了全公司
	 *	{"deptVisibleScopes":[1],"isHidden":false,"userVisibleScopes":[]} 修改微应用可见范围时选择第一级公司
	 *	{"deptVisibleScopes":[6604589,6604590,6604591,6616759,9268861,1],"isHidden":false,"userVisibleScopes":["0440552440847317","044626310037745767"]} 修改微应用可见范围时选择第一级公司和其他几个部门或员工
	 * @param accessToken
	 * @param agentId
	 * @return
	 * @throws OApiException 钉钉接口访问异常
	 */
	public static JSONObject geVisibleScopes(String accessToken, String agentId) throws OApiException {
		String url = Env.OAPI_HOST + "/microapp/visible_scopes?" +
				"access_token=" + accessToken;
		JSONObject args = new JSONObject();
		args.put("agentId", agentId);
		JSONObject response = HttpHelper.httpPost(url, args);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException(I18nMessageUtil.getMessage(I18nStringConstant.MICRO_APPLICATION_ERROR) + ", response = null");
		}
	}
	
	/**
	 * 获取通讯录权限
	 * @param accessToken
	 * @return
	 * @throws OApiException 钉钉接口访问异常
	 * @author kaka
	 * @date 2016年12月27日 下午4:42:13
	 */
	public static JSONObject getAuthScopes(String accessToken) throws OApiException {
		String url = Env.OAPI_HOST + "/auth/scopes?" +
				"access_token=" + accessToken;
		JSONObject response = HttpHelper.httpGet(url);
		
		if (response != null) {
			return response;
		} else {
			throw new OApiResultException(I18nMessageUtil.getMessage(I18nStringConstant.CONTACT_PERMISSION) + ", response = null");
		}
	}
}
